<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/11/1
 * Time: 下午5:07
 */

namespace app\leadertest\controller;


use app\common\controller\AdminBase;
use app\common\model\LoginUsers;
use think\Controller;
use think\Session;
use think\Validate;
use thinkcms\auth\Auth;

class Login extends Controller
{
    //登录显示
    public function index()
    {
        return $this->fetch('login');
    }
    //登录方法
    public function login()
    {
        $request = request();
        if ($request->isAjax()) {
            if ($request->post('remember') == 1) {
                cookie('username', $request->post('username'), 1209600);
                cookie('password', $request->post('password'), 1209600);
            }
            $sdata = [];
            $data = $request->param();
            $validate = new Validate([
                'username|用户名' => 'require',
                'password|密码' => 'require',
                'code|验证码' => 'require|captcha'
            ]);
            if (!$validate->check($data)) {
                $sdata = array('code' => '8', 'message' => $validate->getError());
            } else {
                $User = new LoginUsers();
                $user = $User->where('username', $data['username'])->find();
                if ($this->check_pass_error($user) == 1) {
                    $sdata = array('code' => 6, 'message' => '您的账号已被锁定，请稍后再试');
                } else {
                    if ($user) {
                        if ($user['password'] == MD5($data['password'])&& ($user['level']=="1001"||$user['level']=="1002"||$user['level']=="1010")) {
                            $auth = new Auth();
                            $auth->login($user['id'], $user['username']);
                            $auth->createLog('管理员<spen style=\'color: #1dd2af;\'>[ ' . $data["username"] . ' ]</spen>登陆了后台,', '后台登录', 'login');
                            $url = url('Index/index');
                            Session::set('sysloginid',$user['sysid']);
                            Session::set('level', $user['level']);
                            $sdata = array("code" => 0, "message" => "登录成功", 'url' => $url);
                        } else {
                            $User->where('id', $user['id'])->setInc('pass_error');
                            if ($user['pass_error'] == 4) {
                                $User->save(['lock_time' => time()], ['id' => $user['id']]);
                            }
                            $sdata = array("code" => 1, "message" => "账号或密码错误");
                        }
                    } else {
                        $sdata = array("code" => 1, "message" => "账号或密码错误");
                    }
                }
            }
        } else {
            $sdata = array('code' => 9999, 'message' => "非法访问");
        }
        return json($sdata);
    }

    //检查密码
    private function check_pass_error($user)
    {
        if ($user['pass_error'] >= 5) {
            if ($user['lock_time'] + 600 > time()) {
                $sdata = 1;
            } else {
                $User = new LoginUsers();
                $User->save(['pass_error' => 0, 'lock_time' => 0], ['id' => $user['id']]);
                $sdata = 0;
            }
        } else {
            $sdata = 0;
        }
        return $sdata;
    }

    //电脑端生成二维码
    public function logout()
    {
        Session::clear();
        $auth = new Auth();
        $auth->logout();
        $this->success('退出成功', 'Login/index');
    }

}